﻿Imports System.Math
''' <summary>
'''  风速分析类
''' </summary>
''' <remarks></remarks>
Public Class WindSpeed
    Private m_metd As Met.MetGeneral = New Met.MetGeneral

    ''' <summary>
    ''' 按方向统计
    ''' </summary>
    ''' <remarks></remarks>
    Private m_Arr(-1) As Double
    ''' <summary>
    ''' 按方向来计算风速  
    ''' </summary>
    ''' <remarks></remarks>
    Private m_ArryWindBearing(16, 16) As Double '按方向来计算风速  
    ''' <summary>
    ''' 按每隔几度来计算风速
    ''' </summary>
    ''' <remarks></remarks>
    Private m_ArryWindDegree(16, 36) As Double '按每隔几度来计算风速
    ''' <summary>
    ''' 按方向来计算风频
    ''' </summary>
    ''' <remarks></remarks>
    Private m_ArryWindFrequency(16, 16) As Double '按方向来计算风频
    ''' <summary>
    ''' 按每隔几度来计算风频
    ''' </summary>
    ''' <remarks></remarks>
    Private m_ArryWindFreDegree(16, 36) As Double '按每隔几度来计算风频
    ''' <summary>
    ''' 温度的统计量
    ''' </summary>
    ''' <remarks></remarks>
    Private m_ArryTemperature(12) As Double '温度的统计量
    ''' <summary>
    ''' 风速的统计量
    ''' </summary>
    ''' <remarks></remarks>
    Private m_ArryWindSpeed(12) As Double '风速的统计量
    ''' <summary>
    ''' 风速每小时的数据/四季来统计
    ''' </summary>
    ''' <remarks></remarks>
    Private m_ArryWindSpeedHours(3, 23) As Double '风速每小时的数据/四季来统计


    ''' <summary>
    ''' 按方向来计算风速
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ArryWindBearing() As Double(,)
        Get
            Return m_ArryWindBearing
        End Get
        Set(ByVal value As Double(,))
            m_ArryWindBearing = value
        End Set
    End Property

    ''' <summary>
    ''' 按每隔几度来计算风速
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ArryWindDegree() As Double(,)
        Get
            Return m_ArryWindDegree
        End Get
        Set(ByVal value As Double(,))
            m_ArryWindDegree = value
        End Set
    End Property

    ''' <summary>
    ''' 按方向来计算风频
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ArryWindFrequency() As Double(,)
        Get
            Return m_ArryWindFrequency
        End Get
        Set(ByVal value As Double(,))
            m_ArryWindFrequency = value
        End Set
    End Property

    ''' <summary>
    ''' 按每隔几度来计算风频
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ArryWindFreDegree() As Double(,)
        Get
            Return m_ArryWindFreDegree
        End Get
        Set(ByVal value As Double(,))
            m_ArryWindFreDegree = value
        End Set
    End Property
    ''' <summary>
    ''' 温度的统计量
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ArryTemperature() As Double()
        Get
            Return m_ArryTemperature
        End Get
        Set(ByVal value As Double())
            m_ArryTemperature = value
        End Set
    End Property
    ''' <summary>
    ''' 风速的统计量
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ArryWindSpeed() As Double()
        Get
            Return m_ArryWindSpeed
        End Get
        Set(ByVal value As Double())
            m_ArryWindSpeed = value
        End Set
    End Property
    ''' <summary>
    ''' 风速每小时的数据/四季来统计
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ArryWindSpeedHours() As Double(,)
        Get
            Return m_ArryWindSpeedHours
        End Get
        Set(ByVal value As Double(,))
            m_ArryWindSpeedHours = value
        End Set
    End Property
    Property metd() As Met.MetGeneral
        Get
            Return Me.m_metd
        End Get
        Set(ByVal value As Met.MetGeneral)
            Me.m_metd = value
        End Set
    End Property

    '导入数据
    Public Sub Buttonx()
        Dim AMet As New Met.MetGeneral
        If AMet.OpenMetDataFile() = True Then
            m_metd = AMet
        End If
    End Sub

    'ToDo:风速的统计...如果风向＝0时不参加计算数据差不多，。。有待解决... 风玫瑰

    ''' <summary>
    ''' 按方向来计算风速
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub WindBearing(ByVal nAngleCount As Integer)

        Dim Sum(-1, -1) As Double '用于储存风频的加和
        Dim Count(-1, -1) As Integer
        ReDim Sum(16, nAngleCount)
        ReDim Count(16, nAngleCount)
        ReDim m_Arr(nAngleCount - 1)

        Dim b As Double = 360 / nAngleCount / 2 / 10
        For i As Integer = 0 To m_Arr.Length - 1
            m_Arr(i) = b + i * 2 * b
        Next
        For i As Integer = 0 To Me.m_metd.ArrayMetData.Length - 1
            If m_metd.ArrayMetData(i).m_WindSpeed <> -9999 Then
                Dim imonth As Integer = m_metd.ArrayMetData(i).m_DateTime.Month
                Dim iWindSpeed As Double = m_metd.ArrayMetData(i).m_WindSpeed
                Dim iAngle As Double = m_metd.ArrayMetData(i).m_Vane
                b = 360 / nAngleCount / 2 / 10
                If m_metd.ArrayMetData(i).m_Vane <> 80 Then '非静风统计各方向的平均风速
                    'N方向上的统计结果-----------------
                    If iAngle >= 0 And iAngle <= b Or iAngle > 36 - b And iAngle <= 36 Then
                        Count(imonth - 1, 0) = Count(imonth - 1, 0) + 1
                        Sum(imonth - 1, 0) += m_metd.ArrayMetData(i).m_WindSpeed
                        If imonth >= 3 And imonth <= 5 Then
                            Count(13, 0) += 1
                            Sum(13, 0) += m_metd.ArrayMetData(i).m_WindSpeed
                        End If
                        If imonth >= 6 And imonth <= 8 Then
                            Count(14, 0) += 1
                            Sum(14, 0) += m_metd.ArrayMetData(i).m_WindSpeed
                        End If
                        If imonth >= 9 And imonth <= 11 Then
                            Count(15, 0) += 1
                            Sum(15, 0) += m_metd.ArrayMetData(i).m_WindSpeed
                        End If
                        If imonth = 12 Or (imonth >= 1 And imonth <= 2) Then
                            Count(16, 0) += 1
                            Sum(16, 0) += m_metd.ArrayMetData(i).m_WindSpeed
                        End If
                        Count(12, 0) = Count(12, 0) + 1
                        Sum(12, 0) += m_metd.ArrayMetData(i).m_WindSpeed
                    Else
                        '除N方向以外的统计结果
                        For j As Integer = 1 To Me.m_Arr.Length - 1
                            If iAngle <= m_Arr(j) And b < iAngle Then
                                '统计1-12月各风向的风速和出现次数
                                Count(imonth - 1, j) = Count(imonth - 1, j) + 1
                                Sum(imonth - 1, j) += m_metd.ArrayMetData(i).m_WindSpeed
                                '统计全年各风向的风速和出现次数
                                Count(12, j) = Count(12, j) + 1
                                Sum(12, j) += m_metd.ArrayMetData(i).m_WindSpeed

                                '统计全年中每个季节程度数的风速和出现的次数
                                If imonth >= 3 And imonth <= 5 Then
                                    Count(13, j) += 1
                                    Sum(13, j) += m_metd.ArrayMetData(i).m_WindSpeed
                                End If
                                If imonth >= 6 And imonth <= 8 Then
                                    Count(14, j) += 1
                                    Sum(14, j) += m_metd.ArrayMetData(i).m_WindSpeed
                                End If
                                If imonth >= 9 And imonth <= 11 Then
                                    Count(15, j) += 1
                                    Sum(15, j) += m_metd.ArrayMetData(i).m_WindSpeed
                                End If
                                If imonth = 12 Or (imonth >= 1 And imonth <= 2) Then
                                    Count(16, j) += 1
                                    Sum(16, j) += m_metd.ArrayMetData(i).m_WindSpeed
                                End If
                            End If
                            b = m_Arr(j)
                        Next
                    End If
                End If
                '计算平均值

                Count(imonth - 1, nAngleCount) = Count(imonth - 1, nAngleCount) + 1
                Sum(imonth - 1, nAngleCount) += m_metd.ArrayMetData(i).m_WindSpeed

                Count(12, nAngleCount) = Count(12, nAngleCount) + 1
                Sum(12, nAngleCount) += m_metd.ArrayMetData(i).m_WindSpeed

                If imonth >= 3 And imonth <= 5 Then
                    Count(13, nAngleCount) += 1
                    Sum(13, nAngleCount) += m_metd.ArrayMetData(i).m_WindSpeed
                End If
                If imonth >= 6 And imonth <= 8 Then
                    Count(14, nAngleCount) += 1
                    Sum(14, nAngleCount) += m_metd.ArrayMetData(i).m_WindSpeed
                End If
                If imonth >= 9 And imonth <= 11 Then
                    Count(15, nAngleCount) += 1
                    Sum(15, nAngleCount) += m_metd.ArrayMetData(i).m_WindSpeed
                End If
                If imonth = 12 Or (imonth >= 1 And imonth <= 2) Then
                    Count(16, nAngleCount) += 1
                    Sum(16, nAngleCount) += m_metd.ArrayMetData(i).m_WindSpeed
                End If
            End If
        Next
        '统计平均值
        If nAngleCount = 16 Then
            For i As Integer = 0 To 16
                For j As Integer = 0 To 16
                    If Count(i, j) > 0 Then
                        Me.m_ArryWindBearing(i, j) = Round(Sum(i, j) / Count(i, j) / 10, 2)
                    End If
                Next
                '年平均风速的变化图
                If i <= 12 Then
                    Me.m_ArryWindSpeed(i) = Me.m_ArryWindBearing(i, 16)
                End If
            Next
        ElseIf nAngleCount = 36 Then
            For i As Integer = 0 To 16
                For j As Integer = 0 To 36
                    If Count(i, j) > 0 Then
                        Me.m_ArryWindDegree(i, j) = Round(Sum(i, j) / Count(i, j) / 10, 2)
                    End If
                Next
                '年平均风速的变化图
                If i <= 12 Then
                    Me.m_ArryWindSpeed(i) = Me.m_ArryWindDegree(i, 36)
                End If
            Next
        End If
    End Sub

    ''' <summary>
    ''' 按风的方向和度数统计风频***********
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub WindFrequency(ByVal nAngleCount As Integer)
        '风频的数组
        Dim Sum(-1) As Double '用于储存风频的加和
        Dim Count(-1, -1) As Integer
        ReDim Sum(nAngleCount)
        ReDim Count(16, nAngleCount)

        ReDim m_Arr(nAngleCount - 1)
        Dim b As Double = 360 / nAngleCount / 2 / 10
        For i As Integer = 0 To m_Arr.Length - 1
            m_Arr(i) = b + i * 2 * b
        Next
        For i As Integer = 0 To Me.m_metd.ArrayMetData.Length - 1
            If m_metd.ArrayMetData(i).m_Vane <> 99 Then '非缺失
                Dim imonth As Integer = m_metd.ArrayMetData(i).m_DateTime.Month
                Dim iAngle As Double = m_metd.ArrayMetData(i).m_Vane
                Dim windSpeed As Double = m_metd.ArrayMetData(i).m_WindSpeed
                If m_metd.ArrayMetData(i).m_Vane <> 80 And windSpeed >= 0.3 Then '非静风统计
                    If (windSpeed > 0) Then '如果风速大于0，并且不能为静，就统计方向
                        '0度方向上的统计结果--------------
                        b = 360 / nAngleCount / 2 / 10
                        If iAngle >= 0 And iAngle <= b Or iAngle > 36 - b And iAngle <= 36 Then
                            Count(imonth - 1, 0) = Count(imonth - 1, 0) + 1
                            Count(12, 0) += 1
                            '统计全年中每个季节程度数的风速和出现的次数
                            If imonth >= 3 And imonth <= 5 Then
                                Count(13, 0) += 1
                            End If
                            If imonth >= 6 And imonth <= 8 Then
                                Count(14, 0) += 1
                            End If
                            If imonth >= 9 And imonth <= 11 Then
                                Count(15, 0) += 1
                            End If
                            If imonth = 12 Or (imonth >= 1 And imonth <= 2) Then
                                Count(16, 0) += 1
                            End If
                        Else '非0度方向统计
                            '除N方向以外的统计结果
                            For j As Integer = 1 To Me.m_Arr.Length - 1
                                If iAngle > b And iAngle <= m_Arr(j) Then
                                    Count(imonth - 1, j) = Count(imonth - 1, j) + 1
                                    Count(12, j) += 1
                                    '统计全年中每个季节程度数的风速和出现的次数
                                    If imonth >= 3 And imonth <= 5 Then
                                        Count(13, j) += 1
                                    End If
                                    If imonth >= 6 And imonth <= 8 Then
                                        Count(14, j) += 1
                                    End If
                                    If imonth >= 9 And imonth <= 11 Then
                                        Count(15, j) += 1
                                    End If
                                    If imonth = 12 Or (imonth >= 1 And imonth <= 2) Then
                                        Count(16, j) += 1
                                    End If
                                End If
                                b = m_Arr(j)
                            Next
                        End If

                    End If
                Else '统计为静风
                    Count(imonth - 1, nAngleCount) = Count(imonth - 1, nAngleCount) + 1
                    Count(12, nAngleCount) += 1
                    '统计全年中每个季节程度数的风速和出现的次数
                    If imonth >= 3 And imonth <= 5 Then
                        Count(13, nAngleCount) += 1
                    End If
                    If imonth >= 6 And imonth <= 8 Then
                        Count(14, nAngleCount) += 1
                    End If
                    If imonth >= 9 And imonth <= 11 Then
                        Count(15, nAngleCount) += 1
                    End If
                    If imonth = 12 Or (imonth >= 1 And imonth <= 2) Then
                        Count(16, nAngleCount) += 1
                    End If

                End If
                Sum(imonth - 1) += 1 '统计每个月风的总频率
                '统计每个季度的风速和出现的次数
                If imonth >= 3 And imonth <= 5 Then
                    Sum(13) += 1
                End If
                If imonth >= 6 And imonth <= 8 Then
                    Sum(14) += 1
                End If
                If imonth >= 9 And imonth <= 11 Then
                    Sum(15) += 1
                End If
                If imonth = 12 Or (imonth >= 1 And imonth <= 2) Then
                    Sum(16) += 1
                End If
                '统计全年所有的风速和出现次数
                Sum(12) += 1
            End If
        Next
        '统计平均值
        If nAngleCount = 16 Then
            For i As Integer = 0 To 16
                For j As Integer = 0 To 16
                    If Count(i, j) > 0 Then
                        Me.m_ArryWindFrequency(i, j) = Round(Count(i, j) / Sum(i), 4) * 100
                    End If
                Next
            Next
        ElseIf nAngleCount = 36 Then
            For i As Integer = 0 To 16
                For j As Integer = 0 To 36
                    If Count(i, j) > 0 Then
                        Me.m_ArryWindFreDegree(i, j) = Round(Count(i, j) / Sum(i), 4) * 100
                    End If
                Next
            Next
        End If
    End Sub

    ''' <summary>
    ''' 温度的统计量
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub TemperatureStat()
        Dim Sum(12) As Double '用于储存温度的加和
        Dim Count(12) As Integer '用于存储温度的测量次数
        For i As Integer = 0 To Me.m_metd.ArrayMetData.Length - 1
            If m_metd.ArrayMetData(i).m_DryTemp <> 999 Then
                Dim imonth As Integer = m_metd.ArrayMetData(i).m_DateTime.Month
                Dim ilowcloud As Integer = m_metd.ArrayMetData(i).m_DryTemp
                '统计每个月的温度的加和
                Sum(imonth - 1) += Me.m_metd.ArrayMetData(i).m_DryTemp
                '每个月温度测量的次数
                Count(imonth - 1) = Count(imonth - 1) + 1
                '统计全年的温度
                Sum(12) += Me.m_metd.ArrayMetData(i).m_DryTemp
                Count(12) += 1
            End If
        Next
        '计算平均温度
        For i As Integer = 0 To 12
            If Count(i) > 0 Then
                Me.ArryTemperature(i) = Round(Sum(i) / Count(i) / 10, 2)
            End If
        Next
    End Sub

    ''' <summary>
    ''' 风速每小时的数据统计/四季的方法
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub WindSpeedHoursData()
        Dim ArrySunCount(3, 23) As Integer '统计的次数数组
        Dim ArrySun(3, 23) As Double '数据总和
        For i As Integer = 0 To Me.m_metd.ArrayMetData.Length - 1
            If m_metd.ArrayMetData(i).m_WindSpeed <> -9999 Then
                Dim imonth As Integer = m_metd.ArrayMetData(i).m_DateTime.Month
                Dim iHourth As Integer = m_metd.ArrayMetData(i).m_DateTime.Hour
                Dim iWindSpeed As Double = m_metd.ArrayMetData(i).m_WindSpeed '风速
                '统计风速每个季节第N时时出现的次数及风速总和
                If imonth >= 3 And imonth <= 5 Then
                    ArrySunCount(0, iHourth) += 1
                    ArrySun(0, iHourth) = ArrySun(0, iHourth) + iWindSpeed
                End If
                If imonth >= 6 And imonth <= 8 Then
                    ArrySunCount(1, iHourth) += 1
                    ArrySun(1, iHourth) = ArrySun(1, iHourth) + iWindSpeed
                End If
                If imonth >= 9 And imonth <= 11 Then
                    ArrySunCount(2, iHourth) += 1
                    ArrySun(2, iHourth) = ArrySun(2, iHourth) + iWindSpeed
                End If
                If imonth = 12 Or (imonth >= 1 And imonth <= 2) Then
                    ArrySunCount(3, iHourth) += 1
                    ArrySun(3, iHourth) = ArrySun(3, iHourth) + iWindSpeed
                End If
            End If
        Next
        '统计平均每季每小时的平均风速
        For j As Integer = 0 To 3
            For k As Integer = 0 To 23
                If ArrySunCount(j, k) <> 0 Then
                    ArryWindSpeedHours(j, k) = Math.Round(ArrySun(j, k) / ArrySunCount(j, k) / 10, 2)
                Else
                    ArryWindSpeedHours(j, k) = 0
                End If
            Next
        Next
    End Sub
End Class
